package 网络编程;

import java.io.*;
import java.net.Socket;

public class kehuduan {
    public static void main(String[] args) {
        try {
            //1.创建Socket对象，参数IP 和端口---确定了目标路径
            Socket socket=new Socket("localhost",9879);
            //2.从Sockert中获取流
            OutputStream os=socket.getOutputStream();
            BufferedOutputStream bos=new BufferedOutputStream(os);
            //3.通过流传递信息
            String info="用户名：张三；密码：123123";
            bos.write(info.getBytes());
            bos.flush();
            socket.shutdownOutput();

            InputStream is=socket.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            String answer=br.readLine();
            System.out.println(answer);
            //4.关闭流和Socket
            bos.close();
            os.close();
            br.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
